import { ApiCall } from "tsrpc";
import { ReqProductRoom, ResProductRoom } from "../../../../tsrpc/shared/protocols/room/admin/PtlProductRoom";
import { account } from "../../../../module/account/Account";
import { rs } from "../../../../RoomServer";

export default async function (call: ApiCall<ReqProductRoom, ResProductRoom>) {
    // TODO
    // TODO
         //登录令牌不存在
         if (!call.req.__ssoToken) {
            call.error("登录令牌不存在", { code: 'SSO_NoNull' });
            return;
        }
        // TODO
        const roomName = call.req.roomName;
        if (roomName.length == 0) {
            call.error(`房间名不能为空`, { code: 'RoomName_NoNull' });
            return;
        }
    
        var dUser = account.parseSSO(call.req.__ssoToken);
        if (!dUser) {
            call.error(`登录令牌无效`, { code: 'SSO_Invalid' });
            return;
        }
    
        const entity = rs.createRoom(roomName);
    
        call.succ({ roomInfo:{
            roomId: entity.RoomModel.roomId,
            currentNum: entity.RoomModel.users.size,
            maxNum: entity.RoomModel.maxNum,
            roomState: entity.RoomModel.state,
            roomServerAddr: entity.RoomModel.roomServerAddr,
            roomName: roomName
        } });
}